//
//  HELFeedbackListVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/9.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELFeedbackListVC.h"
#import "HELFeedBackModel.h"
#import "BTAdviceVC.h"
#import "HELFeedbackListCell.h"
@interface HELFeedbackListVC ()

@end

@implementation HELFeedbackListVC





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的反馈";
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64);
    [self.view addSubview:self.tableView];
    self.tableView.estimatedRowHeight = 174;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"HELFeedbackListCell" bundle:nil] forCellReuseIdentifier:@"HELFeedbackListCell"];
    
    
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"留言"] style:UIBarButtonItemStyleDone target:self action:@selector(wirteFeedBack)];
    weakSelf_define;
    [self.tableView wsj_HeaderRefreshBlock:^{
        [weakSelf getMyFeedBackList:YES];
    }];
    [self.tableView wsj_FooterRefreshBlock:^{
        [weakSelf getMyFeedBackList:NO];
    }];

    
}

- (void)viewWillAppear:(BOOL)animated{

    [self getMyFeedBackList:YES];
}

- (void)wirteFeedBack{
    
    BTAdviceVC *vc =[[BTAdviceVC alloc] init];
    
    [self.navigationController pushViewController:vc animated:YES];
    

}


- (void)getMyFeedBackList:(BOOL)isRecover{
    static NSUInteger page = 1;
    if (isRecover) {
        page =1;
    }else{
        page ++;
    }
    
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"page"] = @(page);
    params[@"rows"] = @(10);
    
    
    
    
    weakSelf_define;
    self.tableView.loading = YES;
    [[HELNetworkManager sharedMg] GET:helMyfeedbackList parameters:params success:^(id responseObject) {
        
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
            
            if (isRecover) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSInteger totalPage = 0;
            if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]]) {
                
            }else{
                totalPage =[responseObject[@"data"][@"totalPage"] integerValue];
            }
            
            NSInteger currentPage = [responseObject[@"data"][@"page"] integerValue];
            if (currentPage>=totalPage) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer resetNoMoreData];
            }
            
            [weakSelf.dataArray addObjectsFromArray:[HELFeedBackModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
            
            [weakSelf.tableView reloadData];
            
        }
        
        
    } failure:^(NSString *err) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
    }];
    
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HELFeedbackListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HELFeedbackListCell"];
//    cell.courseListModel = self.dataArray[indexPath.row];
//    
//    cell.tag_MycourseList = YES;
    
    weakSelf_define;
    HELFeedBackModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    cell.cellBlock = ^(){
    
        UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认删除 该条反馈?" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *confirm =[UIAlertAction actionWithTitle:@"确认删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakSelf deleteFeedBackWithId:model.id];
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:confirm];
        [alert addAction:cancel];
       
        [weakSelf presentViewController:alert animated:YES completion:nil];
    
    };
    return cell;
    
    
}


- (void)deleteFeedBackWithId:(NSString *)feedBack{

  
    weakSelf_define;
    cwqHud(self.view);
    [[HELNetworkManager sharedMg] DELETE: [NSString stringWithFormat:@"%@/%@",helDeleteFeedBack,feedBack] parameters:nil success:^(id responseObject) {
        [hud hide:YES];
        cwqHudSuccess(@"删除成功!");
        [weakSelf getMyFeedBackList:YES];
    } failure:^(NSString *err) {
        [hud hide:YES];
    }];

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
  
    
}


@end
